package com.mini.controller;

import com.mini.config.CheckLogin;
import com.mini.req.CooperativeFindReq;
import com.mini.req.CooperativeUpdateReq;
import com.mini.req.CooperativeReq;
import com.mini.res.CooperativeLoadRes;
import com.mini.service.CooperativeService;
import com.mini.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 项目名称：traditional-business
 * 类 名 称：CooperativeController
 * 类 描 述：投放合作／销售合作
 * 创建时间：2020/12/2 上午11:47
 * 创 建 人：zhanghuiying
 */
@RestController
@Api(tags = {"合作管理"})
@RequestMapping(value = "/cooperative/")
@Slf4j
public class CooperativeController extends BaseController {

    @Autowired
    private CooperativeService cooperativeService;

    @CheckLogin
    @ApiOperation(value = "删除合作",notes = "删除合作")
    @DeleteMapping(value = "deleteCooperative")
    public BusinessResult<Boolean> deleteCooperative(@ApiParam(value = "管理类型（1-投放合作管理  2-销售合作管理）", required = true) @RequestParam Integer manageType,
                                                     @ApiParam(value = "列表类型（1-申请列表  2-合作列表）", required = true) @RequestParam Integer listType,
                                                     @ApiParam(value = "合作id", required = true)@RequestParam Long id,
                                                     @ApiParam(value = "token", required = true) @RequestHeader String Authorization){
        try {
            Boolean res= cooperativeService.deleteCooperative(manageType,listType,id,Authorization);
            return BusinessResult.createSuccessInstance(res);
        }catch (Exception e){
            log.info("删除合作列表异常,合作id{}"+e.getMessage(),id);
            return BusinessResult.createInstance(1111L,e.getMessage(),false);
        }
    }
    
    @CheckLogin
    @ApiOperation(value = "确认／撤销／解除合作申请",notes = "确认／撤销／解除合作申请")
    @PutMapping(value = "updateCooperative")
    public BusinessResult<Boolean> updateCooperative(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                     @RequestBody CooperativeUpdateReq cooperativeUpdateVO){
        try {
            Boolean res=cooperativeService.updateCooperative(cooperativeUpdateVO,Authorization);
            return BusinessResult.createSuccessInstance(res);
        }catch (Exception e){
            log.info("确认／撤销／解除合作申请异常，参数{}"+e.getMessage(),cooperativeUpdateVO);
            return BusinessResult.createInstance(1111L,e.getMessage(),false);
        }
    }
    
    @CheckLogin
    @ApiOperation(value = "添加合作申请",notes = "添加合作申请")
    @PostMapping(value = "addCooperativeApply")
    public BusinessResult<Boolean> addCooperativeApply(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                       @RequestBody CooperativeReq cooperativeVO){
        try{
            Boolean res=cooperativeService.addCooperativeApply(cooperativeVO,Authorization);
            return BusinessResult.createSuccessInstance(res);
        }catch (Exception e){
            log.info("添加合作申请异常，参数{}"+e.getMessage(),cooperativeVO);
            return BusinessResult.createInstance(1111L,e.getMessage(),false);
        }
    }
    
    @CheckLogin
    @ApiOperation(value = "查询投放合作申请列表",notes = "查询投放合作申请列表")
    @PostMapping(value = "findThrowCooperativeApplyList")
    public BusinessResult<CooperativeLoadRes> findThrowCooperativeApplyList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                            @RequestBody CooperativeFindReq cooperativeFindReq){
        try{
            checkPageParams(cooperativeFindReq);
            CooperativeLoadRes cooperativeLoadVO=cooperativeService.findThrowCooperativeApplyList(1,Authorization,cooperativeFindReq,1);
            if (Objects.isNull(cooperativeLoadVO)){
                cooperativeLoadVO=new CooperativeLoadRes();
            }
            return BusinessResult.createSuccessInstance(cooperativeLoadVO);
        }catch (Exception e){
            log.error(e.getMessage());
            return BusinessResult.createInstance(CommonResultMessage.ERROR,"");
        }
    }

    @CheckLogin
    @ApiOperation(value = "查询投放合作 合作列表",notes = "查询投放合作 合作列表")
    @PostMapping(value = "findThrowCooperativeCooList")
    public BusinessResult<CooperativeLoadRes> findThrowCooperativeCooList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                          @RequestBody CooperativeFindReq cooperativeFindReq){
        try{
            checkPageParams(cooperativeFindReq);
            CooperativeLoadRes cooperativeLoadVO=cooperativeService.findThrowCooperativeApplyList(1,Authorization,cooperativeFindReq,2);
            if (Objects.isNull(cooperativeLoadVO)){
                cooperativeLoadVO=new CooperativeLoadRes();
            }
            return BusinessResult.createSuccessInstance(cooperativeLoadVO);
        }catch (Exception e){
            log.error(e.getMessage());
            return BusinessResult.createInstance(CommonResultMessage.ERROR,"");
        }
    }

    @CheckLogin
    @ApiOperation(value = "查询销售合作申请列表",notes = "查询销售合作申请列表")
    @PostMapping(value = "findSellCooperativeApplyList")
    public BusinessResult<CooperativeLoadRes> findSellCooperativeApplyList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                           @RequestBody CooperativeFindReq cooperativeFindReq){
        try{
            checkPageParams(cooperativeFindReq);
            CooperativeLoadRes cooperativeLoadVO=cooperativeService.findSellCooperativeApplyList(2,Authorization,cooperativeFindReq,1);
            if (Objects.isNull(cooperativeLoadVO)){
                cooperativeLoadVO=new CooperativeLoadRes();
            }
            return BusinessResult.createSuccessInstance(cooperativeLoadVO);
        }catch (Exception e){
            log.error(e.getMessage());
            return BusinessResult.createInstance(CommonResultMessage.ERROR,"");
        }
    }

    @CheckLogin
    @ApiOperation(value = "查询销售合作 合作列表",notes = "查询销售合作 合作列表")
    @PostMapping(value = "findSellCooperativeCooList")
    public BusinessResult<CooperativeLoadRes> findSellCooperativeCooList(@ApiParam(value = "token", required = true) @RequestHeader String Authorization,
                                                                         @RequestBody CooperativeFindReq cooperativeFindReq){
        try{
            checkPageParams(cooperativeFindReq);
            CooperativeLoadRes cooperativeLoadVO=cooperativeService.findSellCooperativeApplyList(2,Authorization,cooperativeFindReq,2);
            if (Objects.isNull(cooperativeLoadVO)){
                cooperativeLoadVO=new CooperativeLoadRes();
            }
            return BusinessResult.createSuccessInstance(cooperativeLoadVO);
        }catch (Exception e){
            log.error(e.getMessage());
            return BusinessResult.createInstance(CommonResultMessage.ERROR,"");
        }
    }
}
